package com.sgb.kjwl.view.ui.adapter.home;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.BigHomeEntity;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.view.ui.activity.device.DeviceListActivity3;
import com.sgb.kjwl.view.ui.activity.leaseAsk.CarListActivity3;
import com.sgb.kjwl.view.ui.activity.leaseAsk.MachineListActivity3;
import com.sgb.kjwl.view.ui.activity.packers.PackersProjectTeamActivity;
import com.sgb.kjwl.view.ui.activity.pick.PickBiddingListActivity;
import com.sgb.kjwl.view.ui.activity.pick.PickCompanyListActivity;
import com.sgb.kjwl.view.ui.activity.pick.PickHallActivity3;
import com.sgb.kjwl.view.ui.activity.pick.PickInfoActivity;
import com.sgb.kjwl.view.ui.activity.pick.PickNoticeActivity;
import com.sgb.kjwl.view.ui.activity.pick.PickRequireListActivity3;
import com.sgb.kjwl.view.ui.activity.pick.PickSigningUpListActivity;
import com.sgb.kjwl.view.ui.activity.pick.PickWinningNoticeActivity;
import com.sgb.kjwl.view.ui.activity.power.PowerListActivity3;
import com.sgb.kjwl.view.ui.activity.transport.CarsInfoActivity3;
import com.sgb.kjwl.view.ui.activity.transport.GoodsCarsActivity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class BigMaterialAdapter2 extends BaseRecycleAdapter<BigHomeEntity> {
    private TextView name1;
    private LayoutInflater mInflater;
    private LinearLayout ll_p;
    private LinearLayout ll_ex;
    private boolean isFolded;
    private BigMaterialChildAdapter bigMaterialChildAdapter;
    private RecyclerView rv;


    public BigMaterialAdapter2(Context context, List<BigHomeEntity> datas) {
        super(context, R.layout.item_big_home2, datas);
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public void convert(ItemViewHolder holder, final BigHomeEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        name1 = holder.getItem().findViewById(R.id.name1);
        rv = holder.getItem().findViewById(R.id.rv);
        ll_p = holder.getItem().findViewById(R.id.ll_p);
        ll_ex = holder.getItem().findViewById(R.id.ll_ex);
        ImageView img_ex = holder.getItem().findViewById(R.id.img_ex);
        if (datas.size() > 1) {
            name1.setVisibility(View.VISIBLE);
            name1.setText(entity.getName());
        }
        img_ex.setImageDrawable(context.getResources().getDrawable(R.mipmap.icn_zk2));
        isFolded = false;
        ll_p.setTag(holder.getAdapterPosition());
        bigMaterialChildAdapter = new BigMaterialChildAdapter(context, entity.getChildList());
        rv.setAdapter(bigMaterialChildAdapter);
        rv.setLayoutManager(new GridLayoutManager(context, 4));
        bigMaterialChildAdapter.setOnItemClick(new AdapterOnItemClick<MenuEntity>() {
            @Override
            public void onItemClick(MenuEntity child, int position) {
                if (entity.getName().contains("材料/商品") || entity.getName().contains("集采服务")) {
                /*    if ("7".equals(entity.getChildList().get(position).getId())) {
                        CommercialConcreteListActivity3.start(context, entity.getName(), entity.getChildList().get(position).getId(), entity.getChildList().get(position).getId());
                    } else {
                        MaterialListActivity3.start(context, entity.getName(), "0", entity.getChildList().get(position).getId());
                    }*/
                    iItemClick.click(child, position);
                } else if (entity.getName().contains("招标/竞价")) {
                    if (entity.getChildList().get(position).getName().equals("招采企业")) {
                        PickCompanyListActivity.start(context);
                    } else if (entity.getChildList().get(position).getName().equals("招标大厅")) {
                        PickHallActivity3.start(context);
                    } else if (entity.getChildList().get(position).getName().equals("招采预告")) {
                        PickRequireListActivity3.start(context);
                    } else if (entity.getChildList().get(position).getName().equals("公告中心")) {
                        PickNoticeActivity.start(context);
                    } else if (entity.getChildList().get(position).getName().equals("招标公告")) {
                        PickSigningUpListActivity.start(context);
                    } else if (entity.getChildList().get(position).getName().equals("竞价采购")) {
                        PickBiddingListActivity.start(context);
                    } else if (entity.getChildList().get(position).getName().equals("招标信息")) {
                        PickInfoActivity.start(context);
                    } else if (entity.getChildList().get(position).getName().equals("招标预公告")) {
                        PickNoticeActivity.start(context);
                    } else if (entity.getChildList().get(position).getName().equals("中标公告")) {
                        PickWinningNoticeActivity.start(context);
                    }
                } else if (entity.getName().contains("租赁市场")) {
                    if (entity.getChildList().get(position).getName().equals("机械")) {
                        MachineListActivity3.start(context);
                    } else if (entity.getChildList().get(position).getName().equals("设备")) {
                        DeviceListActivity3.start(context);
                    } else if (entity.getChildList().get(position).getName().equals("车辆")) {
                        CarListActivity3.start(context);
                    } else {
                        Toast.makeText(context, "功能暂未开放...", Toast.LENGTH_SHORT).show();
                    }
                } else if (entity.getName().contains("..")) {
                    PackersProjectTeamActivity.start(context, entity.getChildList().get(position).getId());
                } else if (entity.getName().contains("工程建设")) {
                    PowerListActivity3.start(context, entity.getChildList().get(position).getId(), entity.getChildList().get(position).getName());
                } else if (entity.getName().contains("物流/货运")) {
                    GoodsCarsActivity.start(context, 0, true);
                } else if (entity.getName().contains("找车信息")) {
                    CarsInfoActivity3.start(context);
                }
            }
        });
        int position = (int) ll_p.getTag();

        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) ll_p.getLayoutParams();
        if (holder.getAdapterPosition() == position) {
            Log.e(entity.getName(), "test " + px2dip(context, ll_p.getHeight()) + "dp");
            if (px2dip(context, ll_p.getHeight()) > 88) {
                ll_ex.setVisibility(View.VISIBLE);
                params.height = dip2px(context, 88);
            } else {
                if (isFolded)
                    params.height = dip2px(context, params.WRAP_CONTENT);
            }
        }
        img_ex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(entity.getName(), holder.getAdapterPosition() + "");
                if (!isFolded) {
                    params.height = dip2px(context, params.WRAP_CONTENT);
                    img_ex.setImageDrawable(context.getResources().getDrawable(R.mipmap.icn_sq2));
                    isFolded = true;
                } else {
                    params.height = dip2px(context, 88);
                    img_ex.setImageDrawable(context.getResources().getDrawable(R.mipmap.icn_zk2));
                    isFolded = false;
                }
                ll_p.setLayoutParams(params);
            }
        });

    }

    private int dip2px(Context context, float dipValue) {
        Resources r = context.getResources();
        return (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, dipValue, r.getDisplayMetrics());
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public IItemClick iItemClick;

    public interface IItemClick {
        void click(MenuEntity child, int position);
    }

    public void onTypeClick(IItemClick iItemClick) {
        this.iItemClick = iItemClick;
    }

}
